package app;

import com.alibaba.fastjson.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import pojo.PersonEntity;
import pojo.PersonForJson;
import pojo.PersonForXML;

public class Test {

  public static void main(String[] args) throws IOException, JAXBException {
    protobuf();
    json();
    xml();
  }

  private static void xml() throws JAXBException {
    PersonForXML personForXML = new PersonForXML(1, "ant", "ghb@soecode.com");
    JAXBContext context = JAXBContext.newInstance(PersonForXML.class);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    marshaller.marshal(personForXML, baos);

    String xmlObj = new String(baos.toByteArray());
    System.out.println("======XML size=====");
    System.out.println(xmlObj);
    System.out.println(xmlObj.getBytes().length);
    System.out.println("========================\n");
  }

  private static void json() {
    PersonForJson personForJson = new PersonForJson(1, "ant", "ghb@soecode.com");
    System.out.println("======Json size=====");
    String json = JSONObject.toJSONString(personForJson);
    System.out.println(json);
    System.out.println(json.getBytes().length);
    System.out.println("========================\n");
  }

  private static void protobuf() {
    PersonEntity.Person.Builder builder = PersonEntity.Person.newBuilder();
    builder.setId(1);
    builder.setName("ant");
    builder.setEmail("ghb@soecode.com");
    PersonEntity.Person person = builder.build();

    System.out.println("======Protobuf size=====");
    System.out.println(person.toByteString());
    System.out.println(person.toByteArray().length);
    System.out.println("========================\n");
  }
}